using System;
using System.Configuration;
using System.IO;
using System.Text;
using System.Web.UI;
using Channelvn.Cached.Common;

namespace Channelvn.Cached.Base
{
    public class ControlBase : UserControl
    {
        public ControlBase()
        {
            this.m_ManualUpdate = false;
            this.isTinChiTiet_Co_Du_Lieu = true;
        }
        private bool m_ManualUpdate;
        public bool ManualUpdate
        {
            get { return this.m_ManualUpdate; }
            set { this.m_ManualUpdate = value; }
        }

        private bool isTinChiTiet_Co_Du_Lieu;
        public bool IsTinChiTiet_Co_Du_Lieu
        {
            get { return this.isTinChiTiet_Co_Du_Lieu; }
            set { this.isTinChiTiet_Co_Du_Lieu = value; }
        }

        private string m_ControlCacheName;
        public string ControlCacheName
        {
            get { return this.m_ControlCacheName; }
            set { this.m_ControlCacheName = value; }
        }

        private CacheSettings.ControlSetting m_ControlCacheSetting;
        public CacheSettings.ControlSetting ControlCacheSetting
        {
            get { return this.m_ControlCacheSetting; }
            set { this.m_ControlCacheSetting = value; }
        }

        private string m_ParentCategory;
        public string ParentCategory
        {
            get { return this.m_ParentCategory; }
            set { this.m_ParentCategory = value; }
        }

        public void SetProperty(string key, object value)
        {
            System.Reflection.PropertyInfo property = this.GetType().GetProperty(key);
            if (null != property)
            {
                property.SetValue(this, Convert.ChangeType(value, property.PropertyType), null);
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            StringBuilder strBuilder = new StringBuilder();
            using (StringWriter strWriter = new StringWriter(strBuilder))
            {
                using (HtmlTextWriter htmlWriter = new HtmlTextWriter(strWriter))
                {
                    base.Render(htmlWriter);
                    string html = strBuilder.ToString();
                    writer.Write(html);

                    if (isTinChiTiet_Co_Du_Lieu && !IsPostBack && ((ControlCacheSetting.EnableCache && ControlCacheSetting.UpdateCacheByWeb) || ManualUpdate) && CacheController.IsAllowDistributedCached)
                    {
                        if (!string.IsNullOrEmpty(ControlCacheSetting.Category))
                        {
                            CacheController.Add(ControlCacheSetting.Category, ControlCacheName, html, ControlCacheSetting.CacheExpire);
                        }
                        else
                        {
                            CacheController.Add(GetParentCategoryID(), ControlCacheName, html, ControlCacheSetting.CacheExpire);
                        }
                    }
                }
            }
        }

        private string GetParentCategoryID()
        {
            if (ParentCategory == Constants.PARENT_CATEGORY_FOR_NEWS_DETAIL_CACHE ||
                ParentCategory == Constants.PARENT_CATEGORY_FOR_NEWS_DETAIL_CACHE ||
                ParentCategory == Constants.PARENT_CATEGORY_FOR_DATA_CACHE)
            {
                return ParentCategory;
            }
            else
            {
                return CacheController.ParentCategory;
            }
        }
    }
}
